import 'dart:math';

import 'package:flutter/material.dart';

import '../shape/indicator_painter.dart';
import 'controller.dart';

/// BallClipRotate.
class $LoadingBallClipRotate extends StatefulWidget {
  const $LoadingBallClipRotate({Key? key}) : super(key: key);

  @override
  State<$LoadingBallClipRotate> createState() => _$LoadingBallClipRotateState();
}

class _$LoadingBallClipRotateState extends State<$LoadingBallClipRotate>
    with SingleTickerProviderStateMixin, $LoadingIndicatorController {
  static const _durationInMills = 750;

  late AnimationController _animationController;
  late Animation<double> _scaleAnimation;
  late Animation<double> _rotateAnimation;

  @override
  List<AnimationController> get animationControllers => [_animationController];

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: _durationInMills),
    );
    _scaleAnimation = TweenSequence([
      TweenSequenceItem(tween: Tween(begin: 1.0, end: 0.6), weight: 1),
      TweenSequenceItem(tween: Tween(begin: 0.6, end: 1.0), weight: 1),
    ]).animate(CurvedAnimation(
      parent: _animationController,
      curve: Curves.linear,
    ));
    _rotateAnimation =
        Tween(begin: 0.0, end: 2 * pi).animate(CurvedAnimation(parent: _animationController, curve: Curves.linear));

    _animationController.repeat();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: _animationController,
      builder: (_, child) {
        return Transform(
          alignment: Alignment.center,
          transform: Matrix4.identity()
            ..scale(_scaleAnimation.value)
            ..rotateZ(_rotateAnimation.value),
          child: child,
        );
      },
      child: const $LoadingIndicatorShapeWidget(shape: $LoadingShape.ringThirdFour),
    );
  }
}
